package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.User;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsWave;
import com.jy.wms.service.WmsWaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: WmsWaveController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author syh
 * @Date Mon Oct 23 15:32:33 CST 2017
 */
@Controller
@RequestMapping("/wmswave")
@Api(value = "/wmswave", description = "关于创建波次操作")
public class  WmsWaveController extends BaseController<WmsWaveService,WmsWave> {
    /**
     * 波次新增
     * @param setAndModifyModel
     * @param session
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "波次信息 创建波次信息",response = JSONObject.class, notes = "POST创建波次信息")
    public @ResponseBody JSONObject  addWmsLibrarypositiongroup(@RequestBody SetAndModifyModel setAndModifyModel , HttpSession session) {
        JSONObject json = new JSONObject();
        WmsWave entity=new WmsWave();
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            User user=userinfo.getUser();
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            String sshzid=null;
            if(hz.size()>0){
                sshzid=hz.get(0).getHzid();
            }
            String bh=this.getBcBh(session);
            WmsWave entitys=baseService.searchAdd(bh,ckid,sshzid);
            if(entitys!=null){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.info.toString(),"创建波次号重复","波次号重复","");
            }else{
                entity.setId(UUIDUtils.uuid());
                entity.setCkid(ckid);
                entity.setSshzid(sshzid);
                entity.setSh("0");
                entity.setBh(bh);
                entity.setCjr(user.getName());
                baseService.baseInsert(entity,setAndModifyModel.getIds());
            }
        }catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"创建波次成功","", JSONObject.fromObject(entity).toString());
        }catch (RuntimeException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"创建波次失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"创建波次失败","创建波次失败","");
        }
        return json;
    }

    /**
     * 波次号生成
     * @param session
     * @return
     */
    @ApiOperation(value = "波次 生成波次号",response = JSONObject.class, notes = "GET生成波次号")
    @GetMapping(value = "getBcBh")
    public @ResponseBody
    String getBcBh(HttpSession session) {
        String bcBh = null;
        try {
            /**
             * 获取当前仓库
             */
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            /**
             * 取仓库id ckid
             */
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            /**
             * 获取当前货主id  owner
             */
            String owner = null;
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                owner = hz.get(0).getHzid();
            }

            bcBh = baseService.creatBcBh(ckid, owner);
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bcBh;
     }
  }

